﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using RooHui.OA.Biz;

namespace RooHui.OA.WebLogic
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class RoleFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string userName = filterContext.HttpContext.User.Identity.Name;
            if (String.IsNullOrEmpty(userName))
            {
                filterContext.Result = new RedirectResult("/account/login");
                return;
            }
            string url = filterContext.HttpContext.Request.Path.ToLower();
            var moduleList = ModuleBiz.GetModelListByUser(userName);
            if (!moduleList.Select(c => c.Url).Contains(url))
            {
                filterContext.Result = new RedirectResult("/home/index");
                return;
            }
            base.OnActionExecuting(filterContext);
        }
    }
}
